<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <!--
  -->
  <title>Closure Unit Tests - goog.testing.editor.TestHelper</title>
  <script src="../../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.TagName');
    goog.require('goog.editor.node');
    goog.require('goog.testing.editor.TestHelper');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
<div id="root"></div>
<div id="root2">Root 2</div>
<script type="text/javascript">
  var root = goog.dom.getElement('root');
  var helper;

  function setUp() {
    root.innerHTML = '';
    helper = new goog.testing.editor.TestHelper(root);
  }

  function tearDown() {
    helper.dispose();
  }

  function testSetRoot() {
    helper.setRoot(goog.dom.getElement('root2'));
    helper.assertHtmlMatches('Root 2');
  }

  function testSetupEditableElement() {
    helper.setUpEditableElement();
    assertTrue(goog.editor.node.isEditableContainer(root));
  }

  function testTearDownEditableElement() {
    helper.tearDownEditableElement();
    assertFalse(goog.editor.node.isEditableContainer(root));
  }

  function testFindNode() {
    // Test the easiest case.
    root.innerHTML = 'a<br>b';
    assertEquals(helper.findTextNode('a'), root.firstChild);
    assertEquals(helper.findTextNode('b'), root.lastChild);
    assertNull(helper.findTextNode('c'));
  }

  function testFindNodeDuplicate() {
    // Test duplicate.
    root.innerHTML = 'c<br>c';
    assertEquals('Should return first duplicate', helper.findTextNode('c'),
        root.firstChild);
  }

  function findNodeWithHierarchy() {
    // Test a more complicated hierarchy.
    root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>';
    assertEquals(goog.dom.TagName.DIV,
        helper.findTextNode('a').parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        helper.findTextNode('b').parentNode.tagName);
    assertEquals(goog.dom.TagName.SPAN,
        helper.findTextNode('c').parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        helper.findTextNode('d').parentNode.tagName);
    assertEquals(goog.dom.TagName.DIV,
        helper.findTextNode('e').parentNode.tagName);
  }

  function setUpAssertHtmlMatches() {
    var tag1, tag2;
    if (goog.userAgent.IE) {
      tag1 = goog.dom.TagName.DIV;
    } else if (goog.userAgent.WEBKIT) {
      tag1 = goog.dom.TagName.P;
      tag2 = goog.dom.TagName.BR;
    } else if (goog.userAgent.GECKO) {
      tag1 = goog.dom.TagName.SPAN;
      tag2 = goog.dom.TagName.BR;
    }

    var parent = goog.dom.createDom(goog.dom.TagName.DIV);
    root.appendChild(parent);
    parent.style.fontSize = '2em';
    parent.style.display = 'none';
    if (goog.userAgent.IE || goog.userAgent.GECKO) {
      parent.appendChild(goog.dom.createTextNode('NonWebKitText'));
    }

    if (tag1) {
      var e1 = goog.dom.createDom(tag1);
      parent.appendChild(e1);
      parent = e1;
    }
    if (tag2) {
      parent.appendChild(goog.dom.createDom(tag2));
    }
    parent.appendChild(goog.dom.createTextNode('Text'));
    if (goog.userAgent.WEBKIT) {
      root.firstChild.appendChild(goog.dom.createTextNode('WebKitText'));
    }
  }

  function testAssertHtmlMatches() {
    setUpAssertHtmlMatches();

    helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
        '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
        '</div>[[WEBKIT]]WebKitText');
  }

  function testAssertHtmlMismatchText() {
    setUpAssertHtmlMatches();

    try {
      helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
        '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' +
        '</div>[[WEBKIT]]Extra');
      fail('Should have failed due to mismatched text');
    } catch (e) {
      assertContains('Text should match', e.message);
    }
  }

  function testAssertHtmlMismatchTag() {
    setUpAssertHtmlMatches();

    try {
      helper.assertHtmlMatches('<span style="display: none; font-size: 2em">' +
        '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
        '</span>[[WEBKIT]]Extra');
      fail('Should have failed due to mismatched tag');
    } catch (e) {
      assertContains('Tag names should match', e.message);
    }
  }

  function testAssertHtmlMismatchStyle() {
    setUpAssertHtmlMatches();

    try {
      helper.assertHtmlMatches('<div style="display: none; font-size: 3em">' +
        '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
        '</div>[[WEBKIT]]Extra');
      fail('Should have failed due to mismatched style');
    } catch (e) {
      assertContains('Should have same styles', e.message);
    }
  }

  function testAssertHtmlMismatchOptionalText() {
    setUpAssertHtmlMatches();

    try {
      helper.assertHtmlMatches('<div style="display: none; font-size: 2em">' +
        '[[IE GECKO]]Bad<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
        '</div>[[WEBKIT]]Bad');
      fail('Should have failed due to mismatched style');
    } catch (e) {
      assertContains('Text should match', e.message);
    }
  }
</script>
</body>
</html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
